﻿using Microsoft.Xna.Framework;
using OTAPI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using Terraria;
using Terraria.DataStructures;
using TShockAPI;

namespace TShock_IslandWar
{
    class GameManager
    {
        public readonly static int Red = 1;
        public readonly static int Blue = 3;

        public List<TSPlayer> redTeam = new List<TSPlayer>();
        public List<TSPlayer> blueTeam = new List<TSPlayer>();

        public bool RedSpawn = true;
        public bool BlueSpawn = true;

        public int onlinePlayer = 0;

        public int state = 0;

        //private Timer boradcast = new Timer(1 * 1000);
        //public Queue<string> messages = new Queue<string> ();
        public GameManager()
        {
            //boradcast.AutoReset = true;
            //boradcast.Elapsed += (s, e) =>
            //{
            //    while (messages.Count > 0)
            //    {
            //        TSPlayer.All.SendMessage(messages.Dequeue(),Color.Yellow);
            //    }
            //};
            //boradcast.Start();
        }

        int timer0 = 500;
        int timer1 = 800;
        public void Update(DefaultWorld defaultWorld)
        {
            switch (state)
            {
                case 0:
                    {
                        timer0--;
                        if (onlinePlayer < 2 && timer0 < 0)
                        {
                            TSPlayer.All.SendMessage("当前只有{0}个登录玩家，请等待其他玩家".SFormat(onlinePlayer), Color.Red);
                            timer0 = 500;
                            timer1 = 800;
                        }
                        else if (onlinePlayer >= 2)
                        {
                            if (timer1 == 800)
                            {
                                TSPlayer.All.SendMessage("游戏即将开始", Color.Green);
                                redTeam.Clear();
                                blueTeam.Clear();
                            }
                            timer1--;
                        }
                        if (timer1 < 0)
                        {
                            state = 1;
                            TSPlayer.All.SendMessage("游戏开始", Color.Green);
                            defaultWorld.Reset();
                            for (int i = 0; i < Main.maxItems; i++)
                            {
                                Main.item[i].active = false;
                                TSPlayer.All.SendData(PacketTypes.ItemDrop, "", i);
                            }
                            bool flag = false;
                            for (int i = 0; i < TShock.Players.Length; i++)
                            {
                                TSPlayer player = TShock.Players[i];
                                if (player == null)
                                {
                                    continue;
                                }
                                if (player.IsLoggedIn == false)
                                {
                                    continue;
                                }
                                if (flag)
                                {
                                    redTeam.Add(player);
                                    player.SetData("team", Red);
                                    player.SetTeam(Red);
                                    player.Teleport(defaultWorld.redSpawn.X * 16, (defaultWorld.redSpawn.Y - 3) * 16);
                                    player.SendMessage("你被分配到了红队，保护好你的床", Color.Red);
                                    flag = !flag;
                                }
                                else
                                {
                                    blueTeam.Add(player);
                                    player.SetData("team", Blue);
                                    player.SetTeam(Blue);
                                    player.Teleport(defaultWorld.blueSpawn.X * 16, (defaultWorld.blueSpawn.Y - 3) * 16);
                                    player.SendMessage("你被分配到了蓝队，保护好你的床", Color.Blue);
                                    flag = !flag;
                                }
                            }
                            Commands.HandleCommand(TSPlayer.Server, "/resetplayers all");
                        }
                    }
                    break;
                case 1:
                    {
                        if (RedSpawn == false && redTeam.Count == 0)
                        {
                            state = 2;
                            TSPlayer.All.SendMessage("游戏结束，蓝队胜利！！", Color.Green);
                        }
                        else if (BlueSpawn == false && blueTeam.Count == 0)
                        {
                            state = 2;
                            TSPlayer.All.SendMessage("游戏结束，红队胜利！！", Color.Green);
                        }
                        else
                        {
                            for (int i = 0; i < defaultWorld.itemSpawn.Count; i++)
                            {
                                var x = defaultWorld.itemSpawn[i][0];
                                var y = defaultWorld.itemSpawn[i][1];
                                var itemId = defaultWorld.itemSpawn[i][2];
                                var number = defaultWorld.itemSpawn[i][3];
                                var interval = defaultWorld.itemSpawn[i][4];
                                ref var ai = ref defaultWorld.itemSpawn[i][5];
                                ai--;
                                if (ai > 0)
                                    continue;
                                ai = interval;
                                Item.NewItem(new EntitySource_WorldEvent(), x, y, 16, 16, itemId, number);
                            }
                        }
                    }
                    break;
                case 2:
                    {
                        RedSpawn = true;
                        BlueSpawn = true;
                        state = 0;
                        timer0 = 500;
                        timer1 = 800;
                    }
                    break;
            }
        }
    }
}
